//
//  UserDesHeaderView.m
//  吃货·上海
//
//  Created by 高青松 on 15/11/25.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "UserDesHeaderView.h"
#import "UIImageView+WebCache.h"
#import <ShareSDK/ShareSDK.h>
@implementation UserDesHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self)
    {
        //创建标题栏
        self.titleBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenHeight, 60)];
        //self.titleBgView.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_titleBgView];
        
        //标题栏头像
        self.iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
        self.iconImageView.layer.masksToBounds = YES;
        [self.iconImageView.layer setCornerRadius:21];
        //self.iconImageView.backgroundColor = [UIColor redColor];
        self.iconImageView.image = [UIImage imageNamed:@"textIcon.jpg"];
        [self.titleBgView addSubview:_iconImageView];
        
        //标题栏用户信息
        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 200, 20)];
        self.nameLabel.textAlignment = NSTextAlignmentLeft;
        self.nameLabel.font = [UIFont systemFontOfSize:16];
        self.nameLabel.text = @"随溪缘";
        [self.titleBgView addSubview:_nameLabel];
        
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 35, 200, 15)];
        self.timeLabel.textAlignment = NSTextAlignmentLeft;
        self.timeLabel.font = [UIFont systemFontOfSize:13];
        self.timeLabel.textColor = [UIColor grayColor];
        self.timeLabel.text = @"1天前";
        [self.titleBgView addSubview:_timeLabel];
        
        /*
        self.focusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.focusButton.frame = CGRectMake(KScreenWidth - 70, 10, 60, 30);
        [self.focusButton setTitle:@"+ 关注" forState:UIControlStateNormal];
        self.focusButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [self.focusButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.focusButton.layer.masksToBounds = YES;
        [self.focusButton.layer setCornerRadius:8];
        [self.focusButton.layer setBorderWidth:0.5];
        [self.focusButton.layer setBorderColor:[UIColor blackColor].CGColor];
        [self.titleBgView addSubview:_focusButton];
        */
        
        //发布图片
        self.shareImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 60, KScreenWidth, 206)];
        self.shareImageView.image = [UIImage imageNamed:@"text.jpg"];
        self.shareImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_shareImageView];
        
        //发布内容
        self.shareInfoLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 276, KScreenWidth - 20, 90)];
        self.shareInfoLabel.numberOfLines = 0;
        self.shareInfoLabel.textAlignment = NSTextAlignmentLeft;
        //self.shareInfoLabel.backgroundColor = [UIColor redColor];
        self.shareInfoLabel.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:_shareInfoLabel];
        
        //评论点赞和分享朋友圈背景
        self.forumBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 376, KScreenWidth, 40)];
        //self.forumBgView.backgroundColor = [UIColor cyanColor];
        [self addSubview:_forumBgView];
        
        UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 1)];
        lineLabel.backgroundColor = [UIColor grayColor];
        [self.forumBgView addSubview:lineLabel];
        
        self.commentBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 3, 40)];
        [self.forumBgView addSubview:_commentBgView];
        
        CGFloat x = KScreenWidth / 9;
        CGFloat y = (40 - x / 2) / 2;
        self.commentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x + x / 6, y, x / 2, x / 2)];
        self.commentImageView.image = [UIImage imageNamed:@"comment"];
        [self.commentBgView addSubview:_commentImageView];
        
        /*
        self.commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(x + x / 2, y, x / 2 + 10, x / 2)];
        //self.commentLabel.backgroundColor = [UIColor cyanColor];
        self.commentLabel.textAlignment = NSTextAlignmentCenter;
        self.commentLabel.text = @"111";
        self.commentLabel.font = [UIFont systemFontOfSize:11];
        [self.commentBgView addSubview:_commentLabel];
        */
        
        //竖线
        UILabel *verticalLine = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth / 3, 5, 1, 30)];
        verticalLine.backgroundColor = [UIColor grayColor];
        [self.commentBgView addSubview:verticalLine];
        
        self.pointBgView = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth / 3, 0, KScreenWidth / 3, 40)];
        [self.forumBgView addSubview:_pointBgView];
        
        //点赞手势
        UITapGestureRecognizer *pointTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pointTapAction:)];
        [self.pointBgView addGestureRecognizer:pointTap];
        
        //竖线
        UILabel *pointVerticalLine = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth / 3, 5, 1, 30)];
        pointVerticalLine.backgroundColor = [UIColor grayColor];
        [self.pointBgView addSubview:pointVerticalLine];
        
        self.pointImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x + x / 6, y, x / 2, x / 2)];
        self.pointImageView.image = [UIImage imageNamed:@"point"];
        [self.pointBgView addSubview:_pointImageView];
        /*
        self.pointLabel = [[UILabel alloc]initWithFrame:CGRectMake(x + x / 2, y, x / 2 + 10, x / 2)];
        self.pointLabel.textAlignment = NSTextAlignmentCenter;
        self.pointLabel.text = @"111";
        self.pointLabel.font = [UIFont systemFontOfSize:11];
        [self.pointBgView addSubview:_pointLabel];
        */
        
        //分享
        self.shareBgView = [[UIView alloc]initWithFrame:CGRectMake(2 * KScreenWidth / 3, 0, KScreenWidth, 40)];
        [self.forumBgView addSubview:_shareBgView];
        
        //分享手势
        UITapGestureRecognizer *shareTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(shareTapAction:)];
        [self.shareBgView addGestureRecognizer:shareTap];
        
        //竖线
        UILabel *shareVerticalLine = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth / 3, 5, 1, 30)];
        shareVerticalLine.backgroundColor = [UIColor grayColor];
        [self.shareBgView addSubview:shareVerticalLine];
        
        self.shareToFre = [[UIImageView alloc]initWithFrame:CGRectMake(x + x / 6, y, x / 2, x / 2)];
        self.shareToFre.image = [UIImage imageNamed:@"share"];
        [self.shareBgView addSubview:_shareToFre];
        /*
        self.shareLabel = [[UILabel alloc]initWithFrame:CGRectMake(x + x / 2, y, x / 2 + 10, x / 2)];
        self.shareLabel.textAlignment = NSTextAlignmentCenter;
        self.shareLabel.text = @"分享";
        self.shareLabel.font = [UIFont systemFontOfSize:11];
        [self.shareBgView addSubview:_shareLabel];
        */
        
        UILabel *lineLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, self.shareBgView.frame.size.height - 1, KScreenWidth, 1)];
        lineLabel2.backgroundColor = [UIColor grayColor];
        [self.forumBgView addSubview:lineLabel2];

    }
    return self;
}

-(void)setUserShareModel:(UserShareModel *)userShareModel
{
    //赋值
    self.nameLabel.text = userShareModel.userName;
    
    NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strDate = [dateFormatter stringFromDate:userShareModel.createdAt];
    self.timeLabel.text = strDate;
    
    //判断是否赞过
    BmobUser *bUser = [BmobUser getCurrentUser];
    BmobQuery *bquery = [BmobQuery queryWithClassName:[NSString stringWithFormat:@"Collection%@%@",userShareModel.userName, userShareModel.objectId]];
    NSLog(@"%@",[NSString stringWithFormat:@"Collection%@%@",userShareModel.userName, userShareModel.objectId]);
    [bquery whereKey:@"username" equalTo:bUser.username];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error){
            //进行错误处理
            NSLog(@"%@",error);
        }else{
            //表里有数据
            BmobObject *bObject = [BmobObject objectWithClassName:[NSString stringWithFormat:@"Collection%@%@",userShareModel.userName, userShareModel.objectId]];
            for (BmobObject *obj in array)
            {
                NSString *userString = [obj objectForKey:@"username"];
                if ([userString isEqualToString:bUser.username])
                {
                    bObject = obj;
                }
            }
            if ([[bObject objectForKey:@"username"] isEqualToString:bUser.username])
            {
                self.pointImageView.image = [UIImage imageNamed:@"pointed"];
            }
        }
    }];

    
    [self.shareImageView sd_setImageWithURL:[NSURL URLWithString:userShareModel.photo] placeholderImage:[UIImage imageNamed:@"text.jpg"]];
    
    
    
    //动态返回frame
    self.shareInfoLabel.text = userShareModel.info;
    CGRect shareInfoBounds = [userShareModel.info boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName] context:nil];
    //NSLog(@"%f", shareInfoBounds.size.width);
    self.shareInfoLabel.frame = CGRectMake(10, 276, KScreenWidth - 20, shareInfoBounds.size.height);
    self.forumBgView.frame = CGRectMake(0, self.shareInfoLabel.frame.origin.y + self.shareInfoLabel.frame.size.height + 10, KScreenWidth, 40);
    self.bottomView.frame = CGRectMake(0, self.forumBgView.frame.origin.y + self.forumBgView.frame.size.height, KScreenWidth, 10);
}
//动态返回cell高度
+(CGFloat)heightForCell:(UserShareModel *)userShareModel
{
    CGRect shareInfoBounds = [userShareModel.info boundingRectWithSize:CGSizeMake(KScreenWidth - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName] context:nil];
    return 60 + 206 + 10 + shareInfoBounds.size.height + 10 + 10  + 40;
}

//点赞
-(void)pointTapAction:(UITapGestureRecognizer *)pointTap
{
    NSLog(@"1111");
    // 获取当前登录的用户
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser)
    {
        BmobQuery *bquery = [BmobQuery queryWithClassName:[NSString stringWithFormat:@"Collection%@%@",self.shareModel.userName, self.shareModel.objectId]];
        //查找GameScore表里面id为0c6db13c的数据
        [bquery whereKey:@"username" equalTo:bUser.username];
        [bquery  findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            if (error){
                //进行错误处理
                NSLog(@"%@",error);
                BmobObject *userCollection = [BmobObject objectWithClassName:[NSString stringWithFormat:@"Collection%@%@",self.shareModel.userName, self.shareModel.objectId]];
                [userCollection setObject:bUser.username forKey:@"username"];
                [userCollection saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                    //进行操作
                    if (isSuccessful)
                    {
                        NSLog(@"成功");
                        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"赞!"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                        [alterView show];
                        self.pointImageView.image = [UIImage imageNamed:@"pointed"];
                    }
                    else
                    {
                        NSLog(@"失败");
                        NSLog(@"%@",error);
                    }
                }];
            }else{
                //表里有数据
                BmobObject *bObject = [BmobObject objectWithClassName:[NSString stringWithFormat:@"Collection%@%@",self.shareModel.userName, self.shareModel.objectId]];
                for (BmobObject *obj in array)
                {
                    NSString *userString = [obj objectForKey:@"username"];
                    if ([userString isEqualToString:bUser.username])
                    {
                        bObject = obj;
                    }
                }
                if ([[bObject objectForKey:@"username"] isEqualToString:bUser.username])
                {
                    UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"已经赞过了"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alterView show];
                }
                else
                {
                    BmobObject *userCollection = [BmobObject objectWithClassName:[NSString stringWithFormat:@"Collection%@%@",self.shareModel.userName, self.shareModel.objectId]];
                    [userCollection setObject:bUser.username forKey:@"username"];
                    [userCollection saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        //进行操作
                        if (isSuccessful)
                        {
                            NSLog(@"成功");
                            UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"赞!"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
                            [alterView show];
                            self.pointImageView.image = [UIImage imageNamed:@"pointed"];
                        }
                        else
                        {
                            NSLog(@"失败");
                            NSLog(@"%@",error);
                        }
                    }];
                }
            }
        }];
    }
    else
    {
        NSLog(@"请登录");
        UIAlertView *alterView = [[UIAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"请先登录"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alterView show];
    }
}
//分享方法
-(void)shareTapAction:(UITapGestureRecognizer *)shareTap
{
    //    构造分享的内容
    id<ISSContent> publishContent = [ShareSDK content:self.shareModel.info defaultContent:nil image:[ShareSDK imageWithUrl:self.shareModel.photo] title:self.shareModel.userName url:self.shareModel.photo description:self.shareModel.info mediaType:SSPublishContentMediaTypeNews];
    id<ISSContainer> container = [ShareSDK container];
    
    //    IPad需要写下面这行
    //    [container setIPadContainerWithView:self.model.path arrowDirect:UIPopoverArrowDirectionUp];
    
    [ShareSDK showShareActionSheet:container shareList:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        if (state == SSResponseStateSuccess) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享成功" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
        else if (state == SSResponseStateFail)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"失败:%@",[error errorDescription]] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];

}



@end
